#include "cframelesswidgetbase.h"
#include <qt_windows.h>
#include <Windows.h>
#include <windowsx.h>
#include <QDebug>
#include <QPainter>


CFrameLessWidgetBase::CFrameLessWidgetBase(QWidget *parent)
    : QWidget{parent}
{
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);
    setAttribute(Qt::WA_Hover);
    //setAttribute(Qt::WA_TranslucentBackground);
    this->setMouseTracking(true);
}

// bool CFrameLessWidgetBase::nativeEvent(const QByteArray& eventType, void* message, long* result)
// {
//     MSG* param = static_cast<MSG*>(message);

//     switch (param->message)
//     {
//     case WM_NCHITTEST:
//     {
//         int nX = GET_X_LPARAM(param->lParam) - this->geometry().x();
//         int nY = GET_Y_LPARAM(param->lParam) - this->geometry().y();

//         /*if (childAt(nX, nY) != nullptr)
//             return QWidget::nativeEvent(eventType, message, result);*/

//         if (nX > m_nBorderWidth && nX < this->width() - m_nBorderWidth &&
//             nY > m_nBorderWidth && nY < this->height() - m_nBorderWidth)
//         {
//             if (childAt(nX, nY) != nullptr)
//                 return QWidget::nativeEvent(eventType, message, (qintptr*)result);
//         }

//         if ((nX > 0) && (nX < m_nBorderWidth))
//             *result = HTLEFT;

//         if ((nX > this->width() - m_nBorderWidth) && (nX < this->width()))
//             *result = HTRIGHT;

//         if ((nY > 0) && (nY < m_nBorderWidth))
//             *result = HTTOP;

//         if ((nY > this->height() - m_nBorderWidth) && (nY < this->height()))
//             *result = HTBOTTOM;

//         if ((nX > 0) && (nX < m_nBorderWidth) && (nY > 0)
//             && (nY < m_nBorderWidth))
//             *result = HTTOPLEFT;

//         if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())
//             && (nY > 0) && (nY < m_nBorderWidth))
//             *result = HTTOPRIGHT;

//         if ((nX > 0) && (nX < m_nBorderWidth)
//             && (nY > this->height() - m_nBorderWidth) && (nY < this->height()))
//             *result = HTBOTTOMLEFT;

//         if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())
//             && (nY > this->height() - m_nBorderWidth) && (nY < this->height()))
//             *result = HTBOTTOMRIGHT;

//         return true;
//     }
//     }

//     return false;
// }


CFrameLessWidgetBase::~CFrameLessWidgetBase()
{}

void CFrameLessWidgetBase::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_isMousePressed = true;
        m_dragPosition = event->globalPos() - frameGeometry().topLeft();
        event->accept();
    }
}

void CFrameLessWidgetBase::mouseMoveEvent(QMouseEvent *event)
{
    if (m_isMousePressed) {
        move(event->globalPos() - m_dragPosition);
        event->accept();
    }
}

void CFrameLessWidgetBase::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_isMousePressed = false;
        event->accept();
    }
}

void CFrameLessWidgetBase::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setBrush(Qt::white); // 设置窗口背景为白色
    painter.setPen(Qt::NoPen);
    painter.drawRect(rect());
}

// void CFrameLessWidgetBase::mouseMoveEvent(QMouseEvent *event)
// {
//     if (event->buttons() && Qt::LeftButton&&m_bPressed)
//     {
//         move(event->globalPos() - pLast);
//         event->accept();
//     }

//     if (isMaximized())
//     {
//         return;
//     }

//     int iWidth=this->width();
//     int iHeight=this->height();

//     if(event->pos().x()>iWidth-10&&event->pos().y()>iHeight-10)
//     {
//         this->setCursor(Qt::SizeFDiagCursor);
//         int width=this->width()-(this->width()-event->pos().x());
//         int height=this->height()- (this->height()-event->pos().y());
//         if(m_bresize==true)
//         {
//             this->resize(width,height);
//             qDebug()<<"ggggggg";
//         }

//     }
//     else
//     {
//         this->setCursor(Qt::ArrowCursor);
//     }


// }

// void CFrameLessWidgetBase::mousePressEvent(QMouseEvent *event)
// {
//     int width=this->width();
//     int height=this->height();

//     if(event->pos().x()>width-10&&event->pos().y()>height-10&&event->buttons() == Qt::LeftButton)
//     {
//         clickPos=event->pos();
//         m_bresize=true;
//         qDebug()<<"11111";
//     }
//     else
//     {
//         m_bPressed = true;
//         pLast = event->globalPos() - this->pos();
//         event->accept();
//         qDebug()<<"2222";
//     }

// }

// void CFrameLessWidgetBase::mouseReleaseEvent(QMouseEvent *event)
// {
//     m_bPressed = false;
//     m_bresize=false;
// }


